package com.jay.media.music.ui.music

import android.media.MediaMetadata
import android.media.session.PlaybackState
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.viewModels
import androidx.navigation.NavOptions
import com.jay.base.ui.BaseFragment
import com.jay.media.music.ui.HomeActivity
import com.jay.media.music.ui.R
import com.jay.media.music.ui.databinding.FragmentMiniPlayControlsBinding
import com.jay.media.music.ui.manager.DefaultMusicListener
import com.jay.media.music.ui.manager.MusicClientManager
import com.jay.media.music.ui.play.PlayViewModel

/**
 * Create by xuzhou4
 * Date 2021/11/16
 */
class MiniPlayControlsFragment : BaseFragment(), View.OnClickListener {

    private val playViewModel: PlayViewModel by viewModels()
    private var _binding: FragmentMiniPlayControlsBinding? = null
    private val binding get() = _binding!!
    private var mMusicListener = object : DefaultMusicListener() {
        override fun onMetadataChanged(metadata: MediaMetadata?) {
            metadata?.let {
                playViewModel.setPlayMusic(metadata)
            }
        }

        override fun onPlaybackStateChanged(state: PlaybackState?) {
            playViewModel.setPlayState(state)
        }
    }

    override fun rootView(container: ViewGroup?): View? {
        _binding = FragmentMiniPlayControlsBinding.inflate(layoutInflater)
        return binding.root
    }

    override fun initView(view: View) {
        playViewModel.getPlayMusic().observe(this) {
            binding.songNameTextview.text = it.getString(MediaMetadata.METADATA_KEY_TITLE)
            binding.progressBar.max = it.getLong(MediaMetadata.METADATA_KEY_DURATION).toInt()
        }
        playViewModel.getPlayState().observe(this) {
            binding.playStateButton.setImageResource(if (it.state == PlaybackState.STATE_PLAYING) R.drawable.pause else R.drawable.play)
            binding.progressBar.progress = it.position.toInt()
        }

        binding.playStateButton.setOnClickListener(this)
        binding.songLayout.setOnClickListener(this)
    }

    override fun initData() {
        MusicClientManager.getClient().registerMediaListener(mMusicListener)
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.play_state_button -> {
                MusicClientManager.getClient().playOrPause()
            }
            R.id.show_play_fragment_button -> {
                toPlayFragment()
            }
            R.id.song_layout -> {
                toPlayFragment()
            }
            else -> {
            }
        }
    }

    private fun toPlayFragment() {
        val navOptions = NavOptions.Builder()
            .setEnterAnim(R.anim.bottom_up_anim)
            .setPopExitAnim(R.anim.up_bottom_anim)
            .setLaunchSingleTop(true)
            .build()
        val homeActivity = mActivity as HomeActivity
        homeActivity.navigate(R.id.nav_play, null, navOptions)
    }
}